<template> 
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/img/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体部分 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="collapseFlag?'64px':'200px'">
        <div class="toggleMenu" @click="collapseFlag=!collapseFlag">
        ||||
        </div>
        <el-menu
          background-color="#373d41"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse="collapseFlag"
          apse-transition="false"
          :default-active="activeIndex"
          router
          :collapse-transition="false">
          <!-- active-text-color鼠标经过侧边栏图标和标题变色 -->
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模版 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconList[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
  
            <!-- 二级菜单 -->
            <el-menu-item @click="setActive('/'+subitem.path)" :index="'/'+subitem.path" v-for="subitem in item.children" :key="subitem.id">
              <template slot="title">
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
   
          
        </el-menu>
      </el-aside>
      <!-- 右侧内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {

  data () {
    return {
      menuList:[],
      iconList:{
        '125' : 'iconfont icon-user',
        '103' : 'iconfont icon-tijikongjian',
        '101' : 'iconfont icon-shangpin',
        '102' : 'iconfont icon-danju',
        '145' : 'iconfont icon-baobiao'
        },
        collapseFlag:false,
        activeIndex:''
    }
  },
created(){
this.getMenulist();
this.activeIndex=sessionStorage.getItem('activeIndex');
},
  methods: {
    logout(){
      this.$router.push('/login')
      sessionStorage.removeItem('token')
      this.$mg.success('退出成功')
    },
    async getMenulist(){
      const res=await this.axios.get('menus')
      console.log(res);
      
      if(res.meta.status==200){
        this.menuList=res.data
        console.log(this.menuList);  
      }
      console.log(this.menuList);
    },
    // 刷新页面要保持当前高亮,所以将当前设置高亮的index保存到缓存中
    setActive(index){
      this.activeIndex=index;
      sessionStorage.setItem('activeIndex',index);
    }
  }
}
</script>

<style lang='less' scoped>
  .home-container{
    height:100%;
  }
  .el-header{
    background: #373d41;
    display: flex;
    justify-content: space-between;
    padding:10px 20px;
    div{
      display: flex;
      align-items: center;
      
      span{
        color: #fff;
        font-size: 20px;
        margin-left: 15px;
      }
    }
  }
  .iconfont{
    margin-right: 10px;
  }
  .el-aside{
    background: #373d41;
    
    .toggleMenu{
      background-color: hotpink;
      text-align: center;
      padding: 10px 0;
      color:#fff;
      cursor: pointer;
    }
  }
  .el-main{
    background: #eee;
  }
  .el-menu{
    border-right: none;
  }
</style>
